import time
import pymongo
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


class MongoConfig:
    USERNAME = "baibai"
    PASSWORD = "asdbubi841@1997"
    HOST = "82.157.198.174"
    AUTHSOURCE = 'admin'
    PORT = 27017
    DBNAME = "coolboy"

class Mongodb:
    def __init__(self):
        self.client = pymongo.MongoClient(
            host=MongoConfig.HOST,
            port=MongoConfig.PORT,
            username=MongoConfig.USERNAME,
            password=MongoConfig.PASSWORD,
            authSource=MongoConfig.AUTHSOURCE,
        )
        self.db = self.client[MongoConfig.DBNAME]

    def __getitem__(self, table_name):
        return self.db[table_name]
    

class MailSender:
    def __init__(self):
        self._mongo = Mongodb()
        self.subject = "布零布零"
        self.sender_email = "1137930931@qq.com"  # 你的邮箱地址
        self.receiver_email = "1137930931@qq.com"  # 接收邮件的邮箱地址，可以是你自己的邮箱地址
        self.smtp_server = "smtp.qq.com"  # 邮箱的 SMTP 服务器地址
        self.smtp_port = 465  # SMTP 端口（SSL加密）
        self.sender_password = "hrcyufrhyrccidcg"  # 

    def run(self):
        while True:
            if self._mongo["rent_account_info"].find_one({"status": "approve"}):
                self.send("当前租号列表有人需要审核,请注意!")
            if self._mongo["rent_find_info"].find_one({"status": "approve"}):
                self.send("当前求租列表有人需要审核,请注意!")
            if self._mongo["sell_account_info"].find_one({"status": "approve"}):
                self.send("当前卖号列表有人需要审核,请注意!")    
            time.sleep(60)    

    def send(self, body):
        message = MIMEMultipart()
        message["From"] = self.sender_email
        message["To"] = self.receiver_email
        message["Subject"] = self.subject
        # 添加邮件正文
        message.attach(MIMEText(body, "plain"))
        # 连接 SMTP 服务器
        with smtplib.SMTP_SSL(self.smtp_server, self.smtp_port) as server:
            server.login(self.sender_email, self.sender_password)
            server.send_message(message)

if __name__ == "__main__":
    MailSender().run()